package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.it2297.controller.CompanyController;
import com.it2297.main.CompanyApp;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.UIManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Component;
import javax.swing.Box;

public class CompanyRecoverPassword extends JFrame {
	
	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	

	private JPanel contentPane;
	private JTextField tf_rqn;
	private JTextField tf_rans;
	private JTextField tf_cname;
	private JLabel lbl ;
	private JButton btnNewButton;
	private JButton btnSubmit;
	private JLabel lblqn;
	private JLabel lblans;
	final CompanyController cct= new CompanyController(this); 
	private JTextField tf_pwd;
	private JButton btnNewButton_1;
	private JLabel lblQn;
	private JLabel lblpwd;
	
	public String getCompanyName(){
		return tf_cname.getText();
	}
	public String getRecoveryQuestion(){
		return lblqn.getText();
	}
	public String getRecoveryAnswer(){
		return tf_rans.getText();
	}
	 public void setRecoveryQuestion(String rqn){
		  lblqn.setText(rqn);
	 }
	 public void setRecoveryAnswer(String rans){
		 tf_rans.setText(rans);
	 }
	 public void setPassword(String password){
		// if(lbl.getText().equals("Your password is: " + "")){
		//	 lbl.setVisible(false);
		// }
		// else{
			 lblpwd.setText(password);
		// }
	 }
	 
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanyRecoverPasswordTest frame = new CompanyRecoverPasswordTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public CompanyRecoverPassword() {
		String currentWorkingDirectory = System.getProperty("user.dir");
		setTitle("Company - Password Recovery");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setLocationRelativeTo(null);
		contentPane.setLayout(null);
		
		JLabel lblPasswordRecovery = new JLabel("Password Recovery");
		lblPasswordRecovery.setForeground(new Color(128, 0, 0));
		lblPasswordRecovery.setFont(new Font("Times New Roman", Font.BOLD, 45));
		lblPasswordRecovery.setBounds(314, 83, 394, 71);
		contentPane.add(lblPasswordRecovery);
		
		lblqn = new JLabel("");
		lblqn.setForeground(new Color(128, 0, 0));
		lblqn.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblqn.setBounds(314, 252, 410, 24);
		contentPane.add(lblqn);
		
		lblans = new JLabel("Answer?");
		lblans.setVisible(false);
		lblans.setForeground(new Color(0, 0, 0));
		lblans.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblans.setBounds(314, 287, 67, 24);
		contentPane.add(lblans);
		
		tf_rans = new JTextField();
		tf_rans.setBackground(Color.WHITE);
		tf_rans.setVisible(false);
		tf_rans.setBounds(314, 309, 273, 22);
		contentPane.add(tf_rans);
		tf_rans.setColumns(10);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setVisible(false);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tf_rans.getText().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter all the required fields.");
				}
				else{
					
						cct.validateRecoveryPassword();
						if(lblpwd.getText().equals("")){
							lbl.setVisible(false);
							lblpwd.setVisible(false);
						}
						else{
							lbl.setVisible(true);
							lblpwd.setVisible(true);
						}
				}
			}
		});
		btnSubmit.setFont(new Font("Times New Roman", Font.BOLD, 18));
		btnSubmit.setBounds(314, 342, 98, 30);
		contentPane.add(btnSubmit);
		
		lbl= new JLabel("Your password is: ");
		lbl.setVisible(false);
		lbl.setForeground(new Color(0, 0, 0));
		lbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		lbl.setBounds(314, 383, 145, 30);
		contentPane.add(lbl);
		
		JLabel lblCompanyName = new JLabel("Company Name:");
		lblCompanyName.setForeground(new Color(0, 0, 0));
		lblCompanyName.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblCompanyName.setBounds(314, 177, 115, 22);
		contentPane.add(lblCompanyName);
		
		tf_cname = new JTextField();
		tf_cname.setBackground(Color.WHITE);
		tf_cname.setBounds(314, 198, 273, 22);
		contentPane.add(tf_cname);
		tf_cname.setColumns(10);
		
		btnNewButton = new JButton("Get question");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tf_cname.getText().equals("")){
					JOptionPane.showMessageDialog(null, "Please enter the company name.");
				}
				else{
					lblpwd.setText("");
					lbl.setVisible(false);
					cct.getRecoveryQuestion();
					if(lblqn.getText().equals("")){
						tf_rans.setVisible(false);
						lblans.setVisible(false);
						btnSubmit.setVisible(false);
						lblQn.setVisible(false);
						lbl.setVisible(false);
						
					}
					else{
						
						tf_rans.setVisible(true);
						lblans.setVisible(true);
						btnSubmit.setVisible(true);
						lblQn.setVisible(true);
					}
				}
			}
		});
		btnNewButton.setFont(new Font("Times New Roman", Font.BOLD, 15));
		btnNewButton.setBounds(589, 193, 135, 30);
		contentPane.add(btnNewButton);

		lblQn = new JLabel("Question:");
		lblQn.setForeground(new Color(128, 0, 0));
		lblQn.setVisible(false);
		lblQn.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblQn.setBounds(314, 231, 98, 24);
		contentPane.add(lblQn);
		
		lblpwd = new JLabel("");
		lblpwd.setVisible(false);
		lblpwd.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblpwd.setBounds(469, 383, 135, 30);
		contentPane.add(lblpwd);
		
		JLabel lblimg = new JLabel("");
		lblimg.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/recover.png"));
		lblimg.setHorizontalAlignment(SwingConstants.CENTER);
		lblimg.setBounds(0, 21, 310, 501);
		contentPane.add(lblimg);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 21, 724, 501);
		contentPane.add(panel);

		
		
		
		

		menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 724, 24);
		getContentPane().add(menuBar);
		
		menuHome = new JMenu("Home");
		menuBar.add(menuHome);
		menuhomescreen=new JMenuItem("Home Screen");
		menuhomescreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
			}
		});
		menuHome.add(menuhomescreen);
		
		menuJobAd=new JMenu("Job Advertisement");
		menuBar.add(menuJobAd);
		menuCreateJob=new JMenuItem("Create Job Advertisement");
		menuCreateJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobCreateJob jcj=new JobCreateJob();
				jcj.setLocationRelativeTo(null);
				jcj.setVisible(true);
				closeWindow();
			}
		});
		menuJobAd.add(menuCreateJob);
		
		JSeparator separator_1 = new JSeparator();
		menuJobAd.add(separator_1);
		menuRemoveJob=new JMenuItem("Remove Job Advertisement");
		menuJobAd.add(menuRemoveJob);
		
		JSeparator separator_2 = new JSeparator();
		menuJobAd.add(separator_2);
		menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
		menuViewAllJobsByCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
				cvajbc.setLocationRelativeTo(null);
				cvajbc.setVisible(true);
				cvajbc.setTitle("Company - View All Jobs By Company ");
				closeWindow();
			}
		});
		menuJobAd.add(menuViewAllJobsByCompany);
			
		horizontalGlue = Box.createHorizontalGlue();
		menuBar.add(horizontalGlue);
		
		
		
		lblWelcome = new JLabel();
		lblWelcome.setForeground(new Color(0, 0, 128));
		if (CompanyApp.isUserLogin())
		{
			lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
		}
		else
		{
			lblWelcome.setText("Welcome! Please login to manage your job advertisements.");
		}
		menuBar.add(lblWelcome);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		menuBar.add(horizontalStrut);
		
		
		
		mnProfile = new JMenu("Profile");
		menuBar.add(mnProfile);
		
		mntmUpdateCompany = new JMenuItem("Update Company");
		mntmUpdateCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyUpdateCompany cuc = new CompanyUpdateCompany();
				cuc.setVisible(true);
				cuc.setTitle("Update Company Details");
				closeWindow();
			}
		});
		mnProfile.add(mntmUpdateCompany);
		
		menuExit = new JMenu("Exit");
		menuBar.add(menuExit);
		menuLogout=new JMenuItem("Back to Company Login");
		menuLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		menuExit.add(menuLogout);
		
		
		
		if (CompanyApp.isUserLogin())
		{
			 menuHome.setEnabled(true);
			 menuJobAd.setEnabled(true);
			 mnProfile.setEnabled(true);
			   
		}
		else
		{
			 menuHome.setEnabled(false);
			 menuJobAd.setEnabled(false);
			 mnProfile.setEnabled(false);
		}
		
		
	}
	
	public void closeWindow(){
		this.dispose();
	}
}
